var assert = require('assert');
var logger = using('easynode.framework.Logger').forFile(__filename);
var GenericObject = using('easynode.GenericObject');
var ActionResult = using('easynode.framework.mvc.ActionResult');
var utility = require('utility');
var HTTPUtil = using('easynode.framework.util.HTTPUtil');
var BeanFactory = using('easynode.framework.BeanFactory');
var thunkify = require('thunkify');
var md5 = require('md5');


(function () {
        /**
         * Class VerifyCouponController
         *
         * @class #NAMESPACE#.VerifyCouponController
         * @extends easynode.GenericObject
         * @since 0.1.0
         * @author duansj
         * */
        class VerifyCouponController extends GenericObject {
                /**
                 * 构造函数。
                 *
                 * @method 构造函数
                 * @since 0.1.0
                 * @author zlbbq
                 * */
                constructor() {
                        super();
                        //调用super()后再定义子类成员。
                        this.bookshelf = null;                          //IoC injection
                        this.cache = null;
                        this.util=null;
                        this.requestWarpper=null;
                }

                initialize()
                {
                        this.request=this.requestWarpper.getBaseRequest();
                }



                /**
                 * @api {post} /api/verifyCoupon/list 用户获取优惠券(全部)
                 * @apiName listVerifyCoupon
                 * @apiGroup verifyCoupon
                 *
                 * @apiParam {String} mobile 用户手机号 *
                 * @apiParam {String} cookie　登录或注册返回的set-cookie的值 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                list()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                if (this.validator.isValid()) {
                                        var headers=yield me.util.getRequestHeads(this.p('mobile'));
                                        var response = yield me.request({
                                                uri: "/v1/getCoupon",
                                                form:
                                                {
                                                        "mobile":this.p("mobile"),
                                                        "licenseplate":this.p("licenseplate")
                                                },
                                                headers: headers
                                        });
                                        return me.util.handerResult(response);
                                }
                        }
                }

                /**
                 * @api {post} /api/verifyCoupon/validate 优惠码验证
                 * @apiName validateVerifyCoupon
                 * @apiGroup verifyCoupon
                 *
                 * @apiParam {String} mobile 用户手机号 *
                 * @apiParam {String} cuuid 优惠码 *
                 * @apiParam {String} cookie　登录或注册返回的set-cookie的值 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                validate()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('cuuid').necessary().end();
                                if (this.validator.isValid()) {
                                        var headers=yield me.util.getRequestHeads(this.p('mobile'));
                                        var response = yield me.request({
                                                uri: "/v1/verifyCoupon",
                                                form:
                                                {
                                                        "mobile":this.p("mobile"),
                                                        "cuuid":this.p('cuuid')
                                                },
                                                headers: headers
                                        });
                                        return me.util.handerResult(response);
                                }
                        }
                }


                /**
                 * @api {post} /api/verifyCoupon/share 优惠券分享
                 * @apiName shareVerifyCoupon
                 * @apiGroup verifyCoupon
                 *
                 * @apiParam {String} mobile 用户手机号 *
                 * @apiParam {String} cuuid 要分享的优惠券id *
                 * @apiParam {String} cookie　登录或注册返回的set-cookie的值 *
                 *
                 * @apiSuccess {Number} code 返回码，0 -> 成功
                 * @apiSuccess {Object} result API处理结果
                 * @apiSuccess {String} msg 对应于返回码的文本消息
                 * */
                share()
                {
                        var me=this;
                        return function *()
                        {
                                this.validator.check('mobile').necessary().match(me.util.getRegExps().mobile).end();
                                this.validator.check('cuuid').necessary().end();
                                if (this.validator.isValid()) {
                                        var headers=yield me.util.getRequestHeads(this.p('mobile'));
                                        var response = yield me.request({
                                                uri: "/v1/shareCoupon",
                                                form:
                                                {
                                                        "mobile":this.p("mobile"),
                                                        "cuuid":this.p('cuuid')
                                                },
                                                headers: headers
                                        });
                                        return me.util.handerResult(response);
                                }
                        }
                }


                getClassName() {
                        return EasyNode.namespace(__filename);
                }
        }

        module.exports = VerifyCouponController;
})();